<script setup lang="ts">
import {ref, onMounted} from 'vue'
import {storeToRefs} from 'pinia'
import {useApi} from "@/lib/useApi";
import {useStore} from "@/pinia";

const store = useStore()
const {count, double} = storeToRefs(store)
const {add} = store
const {urlResult} = useApi('http://github.com', 'get')
defineProps<{ msg: string }>()

const reset = () => {
  store.$reset()
}
onMounted(() => {
  //两个超过js最大数相加
  let num1 = '1'
  let num2 = '9'
  const add = (num1: string, num2: string) => {
    let maxLength = Math.max(num1.length, num2.length)
    num1 = num1.padStart(maxLength, '0')
    num2 = num2.padStart(maxLength, '0')
    let exceed = 0;
    let num = []
    for (let i = maxLength - 1; i >= 0; i--) {
      let addNum = +num1[i] + +num2[i] + exceed
      exceed = addNum >= 10 ? 1 : 0
      num[i] = addNum >= 10? addNum - 10 : addNum
      i === 0?(addNum >= 10?num.unshift('1'):''):''
    }
    return num.join('')
  }
})
</script>

<template>
  <h1>{{ msg }}</h1>

  <div class="card">
    <button type="button" @click="add(5)">count is {{ count }}</button>
    <button type="button" @click="reset">double is {{ double }}</button>
    <p>
      Edit
      <code>components/HelloWorld.vue</code> to test HMR
    </p>
  </div>

  <p>
    Check out
    <a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
    >create-vue</a
    >, the official Vue + Vite starter
  </p>
  <p>
    Install
    <a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
    in your IDE for a better DX
  </p>
  <p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>
